package com.sendemail.service;

import com.sendemail.config.Constants;
import com.sendemail.email.EmailModel;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by Studio on 2017/5/11.
 *
 */
@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;


    SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public boolean sendEmail(EmailModel emailModel) throws MessagingException {

        String address = "http://"+emailModel.getAddress();
        address = address.replace(" ","%");
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(Constants.emailFrom);
        message.setTo("692972030@qq.com");//emailModel.getEmail()
        message.setSubject("["+emailModel.getTitle()+"]数据已导出完成，请及时下载查看");
        message.setText(emailModel.getContent()+"\r\n"+address);
        mailSender.send(message);
//        MimeMessage mimeMessage = mailSender.createMimeMessage();
//        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//        helper.setFrom(ExportContansts.emailFrom);
//        helper.setTo(email);
//        helper.setSubject("[Dallas能源管理]数据已导出完成，请及时下载查看");
//
//        String msg = MessageFormat.format("<div style=\"text-align: center;width: 600px;margin: auto;font-size: 30px;\"><a style=\"text-decoration: none\" href=\"{0}\">{1}</a></div>\n" +
//                        "<div style=\"text-align: center\"><img style=\"width: 700px\" src=\"{2}\"></div>\n" +
//                        "<div style=\"text-align: center;width: 600px;margin: auto;margin-top: 10px;padding: 0\">" +
//                        "<a href=\"{3}\" style=\"float: right;text-decoration: none\"></a></div>",
//                "http://"+ address
//        );
//
//        helper.setText(msg,true);
//        mailSender.send(mimeMessage);

        return true;
    }

    public boolean sendTemplateMail(EmailModel emailModel) throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //基本设置.
        helper.setFrom(Constants.emailFrom);//发送者.
        helper.setTo("692972030@qq.com");//接收者.emailModel.getEmail()
        helper.setSubject("["+emailModel.getTitle()+"]数据已导出完成，请及时下载查看");//邮件主题.

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("username", emailModel.getUsername());
        model.put("url", emailModel.getAddress());
        model.put("start", emailModel.getStart());
        model.put("end", emailModel.getEnd());
        model.put("name", emailModel.getName());
        model.put("device_id", emailModel.getDevice_id());
        model.put("created", emailModel.getCreate_at());
        Date nowTime=new Date();
        model.put("email_created", time.format(nowTime));

        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        // 设定去哪里读取相应的ftl模板
        cfg.setClassForTemplateLoading(this.getClass(), "/templates");
        // 在模板文件目录中寻找名称为name的模板文件
        Template template   = cfg.getTemplate("email.ftl");

        String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        helper.setText(html, true);

        mailSender.send(mimeMessage);

        return true;
    }
}
